/**  定义电影院相关的接口 */
const express = require("express");
const router = express.Router();
const Joi = require("joi");
const Response = require("../utils/Response.js");

// 引入mysql连接池
const pool = require("../utils/db.js");

/**
 * 删除热门事件接口*****************************************************************************************************************************
 * @param:
 *   ban_id:   事件id
 * @return:
 *   {code:200, msg:'ok'}
 */
router.post("/banner/del", (req, resp) => {
  let { ban_id } = req.body;

  // 表单验证
  let schema = Joi.object({
    ban_id: Joi.number().required(), // 必填
   
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return; // 结束
  }

  // 执行删除业务
  let sql = "delete from banner_src where ban_id = ?";
  pool.query(sql, [ban_id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok());
  });
});

/**
 * 通过ID查询事件
 * @param:
 *   ban_id:   事件id   ok*******************************************************************************************************
 * @return:
 *   {code:200, msg:'ok', data:{}}
 */
router.get("/banner/query", (req, resp) => {
  let { ban_id } = req.query;
  // 表单验证
  let schema = Joi.object({
    ban_id: Joi.number().required(), // 必填
    
  });
  let { error, value } = schema.validate(req.query);
  if (error) {
    resp.send(Response.error(400, error));
    return; // 结束
  }

  // 执行查询业务
  let sql = "select * from banner_src where ban_id=?";
  pool.query(sql, [ban_id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    if (result && result.length == 0) {
      // 没查到
      resp.send(Response.ok(null));
    } else {
      resp.send(Response.ok(result[0]));
    }
  });
});

/**
 * 查询所有事件**************************************************************************************************************************************
 * @param:
 *   无
 * @return:
 *   {code:200, msg:'ok', data:[{}]}
 */
router.get('/banner', (req, resp) => {
  let sql = "select * from banner_src"
  pool.query(sql, (error, result) => {
    if (error) {
      resp.send(Response.error(500, error))
      throw error;
    }
    resp.send(Response.ok(result))
  })
})

/**
 * 通过日期与电影ID查询有该电影排期的电影院列表
 * 
 */



/**
 * 添加电影院接口**********************************************************************************************************************************************
 * @param:
 *   详见接口文档
 * @return:
 *   {code:200, msg:'ok'}
 */
router.post("/home/banner", (req, resp) => {
  let { ban_id, ban_src, email

  } = req.body; // post请求参数在req.body中

  // 表单验证
  let schema = Joi.object({
    ban_id: Joi.number().required(),
    ban_src: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return; // 结束
  }

  // 表单验证通过，执行添加操作
  let sql = `insert into banner_src (ban_id,ban_src) values (?,?)`;
  pool.query(
    sql,
    [ban_id, ban_src],
    (error, result) => {
      if (error) {
        resp.send(Response.error(500, error));
        throw error;
      }
      resp.send(Response.ok());
    }
  );
});

/**
 * 修改接口******************************************************************************************************************************************
 * @param:
 *   详见接口文档
 * @return:
 *   {code:200, msg:'ok'}
 */
router.post("/banner/update", (req, resp) => {
  let {
    ban_id, ban_src, email
  } = req.body; // post请求参数在req.body中

  // 表单验证
  let schema = Joi.object({
    ban_id: Joi.number().required(),
    ban_src: Joi.string().required(),  

  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return; // 结束
  }

  // 表单验证通过，执行添加操作
  let sql = `update banner_src set  ban_src=?  where ban_id=?`;
  pool.query(
    sql,
    [ban_src, ban_id],
    (error, result) => {
      if (error) {
        resp.send(Response.error(500, error));
        throw error;
      }
      resp.send(Response.ok());
    }
  );
});


// 将router对象导出
module.exports = router;
